package com.example.scribe.backend;

import java.lang.reflect.Method;

final class RuntimeHelper {
    private RuntimeHelper() {}

    static int invokeSameModule(String generatedClassName, String functionName) {
        try {
            Class<?> cls = Class.forName(generatedClassName);
            java.lang.reflect.Method m = cls.getDeclaredMethod(functionName);
            return (int) m.invoke(null);
        } catch (Exception e) {
            throw new RuntimeException("invokeSameModule0 failed: " + generatedClassName + "." + functionName, e);
        }
    }

    static int invokeSameModule(String generatedClassName, String functionName, int arg) {
        try {
            Class<?> cls = Class.forName(generatedClassName);
            Method m = cls.getDeclaredMethod(functionName, int.class);
            return (int) m.invoke(null, arg);
        } catch (Exception e) {
            throw new RuntimeException("invokeSameModule failed: " + generatedClassName + "." + functionName, e);
        }
    }

    static int factorialPattern(String generatedClassName, String functionName, int n) {
        if (n <= 1) return 1;
        int rec = invokeSameModule(generatedClassName, functionName, n - 1);
        return n * rec;
    }
}


